package aj.service.impl;

import aj.bo.AjActivationCode;
import aj.bo.AjBuyRecord;
import aj.bo.AjBuyRecordDetail;
import aj.dao.IAjBuyRecordDao;
import aj.service.AjActivationCodeService;
import aj.service.AjBuyRecordDetailService;
import aj.service.AjBuyRecordService;
import framework.common.Common;
import framework.util.jdkExt._TimeUtil;
import framework.util.queryFilter.service.impl.IBaseServiceImpl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service("AjBuyRecordService")
public class AjBuyRecordServiceImpl extends IBaseServiceImpl<AjBuyRecord, Integer> implements AjBuyRecordService {

    @Resource
    private IAjBuyRecordDao dao;
    @Resource
    private AjBuyRecordDetailService ajBuyRecordDetailService;
    @Resource
    private AjActivationCodeService ajActivationCodeService;

    @Override
    public void add(AjBuyRecord bo, List<AjBuyRecordDetail> ajBuyRecordDetails) {
        ///1.保存购买记录主表
        bo.setJsAmount(ajBuyRecordDetails.size());
        bo.setOperatorTime(_TimeUtil.now("yyyy-MM-dd HH:mm:ss"));
        this.add(bo);

        ///2.保存购买记录明细
        for (AjBuyRecordDetail ajBuyRecordDetail : ajBuyRecordDetails) {
            ajBuyRecordDetail.setBuyLid(bo.getLid());
            ajBuyRecordDetailService.add(ajBuyRecordDetail);

            ///3.生成激活码
            AjActivationCode code = new AjActivationCode();
            code.setBuyDetailLid(ajBuyRecordDetail.getLid());
            code.setJsLid(ajBuyRecordDetail.getJsLid());
            code.setIsUse(Common.IS_NO);
            code.setIsUpgrade(Common.IS_NO);
            code.setCode(UUID.randomUUID().toString());
            ajActivationCodeService.add(code);
        }
    }
}

